package at.guigu.config;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;

public class MyBatisConfiguration {

    @Bean
    public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource) {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        // 相当于设置别名<package name="at.guigu.pojo"/>
        sqlSessionFactoryBean.setTypeAliasesPackage("at.guigu.pojo");
        // 相当于配置数据库连接信息
        sqlSessionFactoryBean.setDataSource(dataSource);
        return sqlSessionFactoryBean;
    }
    // 映射扫描配置类，相当于引入dao包下所有接口对应的SQL映射文件<package name="at.guigu.dao"/>
    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer() {
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setBasePackage("at.guigu.dao");
        return mapperScannerConfigurer;
    }
}